

/************************XML转换为流程图*************************************/

/** 默认配置 */
var configs  = {
	node 	   :{
		width  : 110,//矢量图形宽度
		height : 50,//矢量图形高度
		taskNum: 1,
		endNum : 1,
		subProNum:1,
		forkNum:1,
		joinNum:1,
		signNum:1,
		lineNum:1,
		boolNum:1
	},
	canvasLeft : 158,//155画布离左部距离
	canvasTop  : 31,//31画布离顶部距离
	xml        : null,//js xml对象
	canvas     : null,//创建绘图对象（画布）
	curBtn     : 'select', 
	curNode    : null,//当前选中节点
	curXmlNode : null,//当前选中的XML节点
	workflowId : null,//流程ID
	wfVersionId: null,//流程版本ID
	connections: []
}  

//创建xml对象
function createXml(){
	if(configs.xml == null){
		if(window.ActiveXObject){
			configs.xml = new ActiveXObject("Microsoft.XMLDOM");
		} else if(document.implementation && document.implementation.createDocument){
			configs.xml = document.implementation.createDocument("", "", null); 
		}
		var p = configs.xml.createProcessingInstruction("xml","version='1.0' encoding='UTF-8'");
		configs.xml.appendChild(p);
		var root = configs.xml.createElement("process");
		root.setAttribute("name","新建流程");
		root.setAttribute("xmlns","http://jbpm.org/4.4/jpdl");
		configs.xml.appendChild(root);
		//showNodeProperty();
	}
	
}

/**
 * 根据节点name获取Node
 * @param {} nodeName
 */
function findNodeByTitle(nodeName){
	//console.log('----findNodeByTitle--------nodeName==='+nodeName);
	var _node = null;
	configs.canvas.forEach(function (el) {
		//console.log('el.title====='+el.title);
		if(el.title == nodeName && el.type=='rect'){
			_node = el;
		}
	});
	//console.log('_node==='+_node);
	return _node;
}

function nodeMouseOverOut(_node){
	_node.mouseover(function(e){
		//获取当前数据
		$.ajax({ 
			url: "task/getTask",
			type:'post',
			dataType:'json',
			data:{
				instanceId:$('#instanceId').val(),
				taskName:this.title,
				cachenum:Math.random()
			},
			success: function(result){
				//console.log('------success-----------result----------------------');
				//console.log(result[0]);
				var taskArr = new Array();

				taskArr.push('<tr><th>任务名称</th><td>'+result[0].taskName+'</td></tr>');
				taskArr.push('<tr><th>办 理  人</th><td>'+result[0].transactorName+'</td></tr>');
				taskArr.push('<tr><th>开始时间</th><td>'+result[0].startTime+'</td></tr>');
				taskArr.push('<tr><th>结束时间</th><td>'+result[0].endTime+'</td></tr>');
				taskArr.push('<tr><th>办理类型</th><td>'+'</td></tr>');
				taskArr.push('<tr><th>审核意见</th><td>'+result[0].remarks+'</td></tr>');
				/*
				else {
					taskArr.push('<tr><th>任务状态</th><td>未执行</td></tr>');
				}*/
				

				$(".task-table").empty();
				$(".task-table").append(taskArr.join(''));
			}
		});
		var taskTips = $("#task-tips");
		var _width = e.clientX;
		var _height = e.clientY;
		if(_height > ($('body').height()-taskTips.height())){
			_height = _height-taskTips.height();
		}
		if(_width > ($('body').width()-taskTips.width())){
			_width = _width-taskTips.width();
		}
		
		taskTips.css({   
          	left: _width + 'px',   
          	top: _height + 'px'  
      	});  

		taskTips.css('display', 'block');
	}).mouseout(function(e){
		var taskTips = $("#task-tips");
		
		taskTips.css('display', 'none');  
	});	
}

/**
 * 将XML转换为流程图
 * author：liushx
 */
function xmlToProcess(loadXml){
	//console.log('--------------------------loadXml------------------');
	//console.log(loadXml);
	var isIE = /msie/.test(navigator.userAgent.toLowerCase());
	if(isIE) {
		configs.xml.loadXML(loadXml);
	} else {
		configs.xml = new DOMParser().parseFromString(loadXml,"text/xml"); 
	}
	
	//console.log('--------------------------configs.xml------------------');
	//console.log(configs.xml);
	//获取ROOT节点
	var root = configs.xml.documentElement;
	var nodeTypes = ['start', 'task', 'sign','subPro', 'fork', 'join', 'end', 'decision'];
	for(var i=0;i<nodeTypes.length;i++){
		var rootChilds = root.getElementsByTagName(nodeTypes[i]);
		//console.log(nodeTypes[i] +'-----------------rootChilds.length----------------'+rootChilds.length);
		//console.log(rootChilds);
		for(var j=0; j<rootChilds.length; j++){
			xmltoNode(rootChilds[j], nodeTypes[i]);
		}
	}

	var lines = configs.xml.getElementsByTagName("transition");
	for(i=0;i<lines.length;i++){
		xmltoLine(lines[i]);
	}
	
}

//XML的逆向转换，生成节点
function xmltoNode(child, nodeType){
	//console.log('--------------------------------xmltoNode----------------------');
	//var showNode = true;//是否创建节点
	var locations = '';
	var isIE = /msie/.test(navigator.userAgent.toLowerCase());
	if(isIE) {
		locations = child.getAttribute("g").split(",");
	} else {
		locations = child.attributes.g.value.split(",");
	}
	
	var nodeX = parseInt(locations[0]);
	var nodeY = parseInt(locations[1]);
	var nodeName = child.getAttribute('name');
	var isAudit = child.getAttribute('isAudit');
	//添加矢量图节点
	addVectorNode(nodeX, nodeY, nodeType, nodeName, isAudit);
}

//XML的逆向转换，生成连线及文本节点
function xmltoLine(child){
	var nodeName = child.parentNode.getAttribute("name");

	var _srcRect = findNodeByTitle(nodeName);
	var toName = child.getAttribute("name");
	toName = toName.substring(3, toName.length);
//	console.log(toName);
	var _g = child.getAttribute("g");
	//console.log('toName=========='+toName);
	var _desRect = findNodeByTitle(toName);
	//console.log('_srcRect==='+_srcRect+'	$$_desRect=='+_desRect);
	if(_srcRect != null && _desRect != null){
		//console.log('_srcRect==========');
		//console.log(_srcRect);
		addVectorPath(_srcRect, _desRect, _g);
	}

}
